package com.aischool.webapp.family;

import com.aischool.common.kit.Md5Kit;
import com.aischool.common.kit.VerifyCodeKit;
import com.aischool.common.model.AiParent;
import com.aischool.common.model.SysUser;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiOrgService;
import com.aischool.service.SysUserAppService;
import com.aischool.service.SysUserRoleService;
import com.aischool.service.SysUserService;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/front/family/register")
public class RegisgerController extends BaseFamilyController {
	
	@Inject
	SysUserService sysUserService;
	@Inject
	AiOrgService aiOrgService;
	@Inject
	SysUserAppService sysUserAppService;
	@Inject
	SysUserRoleService sysUserRoleService;

	public void index() {
		render(view_path + "/register.html");
	}
	
	/**
	 * 用户注册
	 */
	public void save() {
		JsonResult jr =new JsonResult();
		
		String username = getPara("username", "");
		String password = getPara("password", "");
		String parent_name = getPara("parent_name", "");
		String checkcode = getPara("verifycode", "");
		String family_code = getPara("family_code", "");
		
		if (StrKit.isBlank(username)) {
			jr.setMsg("请输入登录账户！");
		}else if (StrKit.isBlank(password)) {
			jr.setMsg("请输入密码！");
		}else if (StrKit.isBlank(parent_name)) {
			jr.setMsg("请输入家长姓名！");
		}else if (StrKit.isBlank(checkcode)) {
			jr.setMsg("请输入验证码！");
		}else if (!VerifyCodeKit.checkCode(checkcode, this)) {
			jr.setMsg("验证码不正确！");
		}else if (StrKit.isBlank(family_code)) {
			jr.setMsg("请输入邀请码！");
		}else {
			boolean make_user = true;
			int org_id = 0;
			// 检测手机号是否存在
			Record rdu = sysUserService.getUserByMobile(username);
			if (rdu != null) {
				make_user = false;
				jr.appendMsg("手机号已注册！");
			}
			rdu = sysUserService.getUserByLoginName(username);
			if (rdu != null) {
				make_user = false;
				jr.appendMsg("手机号已注册！");
			}
			// 检测邀请码是否存在
			Record rdorg = aiOrgService.getOrg(family_code, "");
			if (rdorg == null) {
				make_user = false;
				jr.appendMsg("邀请码不存在！请输入正确的邀请码！");
			}
			// 注册
			if (make_user) {
				org_id = rdorg.getInt("org_id");
				// 用户表
				SysUser infou = new SysUser();
				infou.setLoginName(username);
				infou.setLoginPwd(Md5Kit.MD5For8(password));
				infou.setUserMobile(username);
				infou.setUserName(parent_name);
				infou.setRegTime(DateUtil.getTimestamp());
				infou.setUpdateTime(DateUtil.getTimestamp());
				infou.setUserState(1);
				infou.setIsLock(false);
				// 家长表
				AiParent infop = new AiParent();
				infop.setOrgId(org_id);
				infop.setParentName(parent_name);
				infop.setParentMobile(username);
				infop.setAddTime(DateUtil.getTimestamp());
				infop.setUpdateTime(DateUtil.getTimestamp());
				infop.setParentState(1);
				//
				infop.setClassId(0);
				infop.setStudentId(0);
				infop.setKindredId(0);
				// 班级、学生、亲属关系，Inject拦截判断，优先设置家长资料
				
				boolean bf = false;
				
				bf = Db.tx(() -> {
					boolean bft = true; // 添加园长结果
					int user_id = 0;
					bft = infou.save();
					if (bft) {
						user_id = infou.getUserId();
						// 设置用户应用权限
						sysUserAppService.setUserApp(user_id, this.def_app_id);
						// 设置用户应用角色
						sysUserRoleService.setUserAppRole(user_id, this.def_app_id, 3); // 前台用户
						// 添加家长
						infop.setUserId(user_id);
						
						bft = infop.save();
					}
					return bft;
				});
				
				if (bf) {
					jr.setEnum(JsonCodeEnum.Success);
				}else {
					String msg = jr.getMsg();
					jr.setEnum(JsonCodeEnum.Faild);
					jr.appendMsg(msg);
				}
			}
		}
		
		renderJson(jr.Dic);
	}
}
